﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace Hotel_webservice
{
    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        public bool DangNhap(string username, string password)
        {
            KhachSanEntities ks = new KhachSanEntities();
            var count = ks.proc_DangNhap(username, password).FirstOrDefault();
            if (count == 1)
            {
                if (Session["isLogin"] == null)
                {
                    Session.Add("isLogin", "accept");
                    Session.Add("username", username);
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        [WebMethod]
        public List<LoaiPhong> LayDSLoaiPhong()
        {
            KhachSanEntities ks = new KhachSanEntities();
            var res = ks.proc_LayDSLoaiPhong();
            var l = new List<LoaiPhong>();
            l = res.ToList<LoaiPhong>();
            return l;
        }

        [WebMethod]
        public List<HangPhong> LayDSHangPhong()
        {
            KhachSanEntities ks = new KhachSanEntities();
            var res = ks.proc_LayDSHangPhong();
            var l = new List<HangPhong>();
            l = res.ToList<HangPhong>();
            return l;
        }

        [WebMethod]
        public List<LoaiPhong> LayDSLoaiPhongTheoHangPhong(String MaHangPhong)
        {
            KhachSanEntities ks = new KhachSanEntities();
            var res = ks.proc_LayDSLoaiPhongTheoHangPhong(MaHangPhong);
            var l = new List<LoaiPhong>();
            l = res.ToList<LoaiPhong>();
            return l;
        }

        [WebMethod]
        public List<LoaiHangPhong> LayDSLoaiHangPhongHoTro(DateTime NgayBD, DateTime NgayKT)
        {
            var ks = new KhachSanEntities();
            var res = ks.proc_LayDSLoaiHangPhongHoTro(NgayBD, NgayKT);
            var l = res.ToList<LoaiHangPhong>();
            return l;
        }

        [WebMethod]
        public int LaySoLuongPhongTrong(string MaLoaiPhong, string MaHangPhong, DateTime NgayBD, DateTime NgayKT)
        {
            KhachSanEntities ks = new KhachSanEntities();
            int? i = ks.proc_LaySoLuongPhongTrong(MaLoaiPhong, MaHangPhong, NgayBD, NgayKT).FirstOrDefault();
            return i.Value;
        }

        [WebMethod]
        public int LayGiaThanh(String MaLoaiPhong, String MaHangPhong)
        {
            KhachSanEntities ks = new KhachSanEntities();
            int? giaThanh = ks.proc_GiaPhong(MaLoaiPhong, MaHangPhong).FirstOrDefault();
            return giaThanh.Value;
        }

        [WebMethod(EnableSession = true)]
        public String DatPhong(List<LoaiHangPhong> DSLoaiHangPhong, DateTime NgayBD, DateTime NgayKT)
        {
            //string maTK_temp = "CT18357619";
            var ks = new KhachSanEntities();
            string str = "";
            if (Session["isLogin"].ToString().CompareTo("accept") == 0)
            {
                int? tongSoLuong = 0;
                string maDP = ks.proc_LayMaDP().FirstOrDefault();
                foreach (LoaiHangPhong i in DSLoaiHangPhong)
                {
                    tongSoLuong = tongSoLuong + i.soLuong;
                }
                str = ks.proc_ExecDatPhong(tongSoLuong, NgayBD, NgayKT, Session["username"].ToString(), maDP).FirstOrDefault();
                foreach (LoaiHangPhong i in DSLoaiHangPhong)
                {
                    string hang = i.maHang;
                    string loai = i.maLoai;
                    int? soLuong = i.soLuong;
                    ks.proc_ThemDSDatPhong(hang, loai, soLuong, NgayBD, NgayKT, maDP);
                }
                return str;
            }
            else
            {
                return str;
            }
        }
    }
}
